package com.aiyige.page.publish.info.model;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.aiyige.city.model.City;
import com.aiyige.location.model.LocationData;
import com.aiyige.model.moment.backup.DetailBackup;
import com.aiyige.model.moment.backup.TagBackup;
import com.aiyige.model.moment.entity.Article;
import com.aiyige.model.moment.entity.Content;
import com.aiyige.model.moment.entity.Moment;
import com.aiyige.page.interest.model.Interest;
import com.aiyige.page.interest.util.InterestUtil;
import com.aiyige.page.publish.info.adapter.PublishInfoAdapter;
import com.aiyige.page.publish.selectinterest.model.PublishIndustryItem;
import com.aiyige.page.publish.selectinterest.model.PublishInterestItem;
import com.aiyige.page.publish.selectlearntarget.model.SelectLearnTargetItem;
import com.aiyige.page.publish.selectsuitablelearnage.model.SuitableLearnAgeItem;
import com.aiyige.page.publish.selecttag.model.SelectTagItem;
import com.aiyige.page.publish.tag.model.TagItem;
import com.aiyige.page.publish.util.PublishUtil;
import com.aiyige.richtexteditor.model.RTEModel;
import com.aiyige.setup.util.SetupUtil;
import com.aiyige.utils.DummyDataUtil;
import com.aiyige.utils.ListUtil;
import com.aiyige.utils.widget.articleview.model.ArticleNode;
import com.alibaba.fastjson.JSON;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by zhuyakun on 2017/11/16.
 */

public class PublishInfoFormModel implements Parcelable {
    String goodsId;
    String coverUrl;
    boolean coverNeedUpload;
    String title;
    List<ArticleNode> articleNodeList;
    List<Interest> interestList;
    List<Interest> industryList;
    LocationData locationData;
    int operationType;
    RTEModel rteModel;

    public PublishInfoFormModel() {
    }

    private PublishInfoFormModel(Builder builder) {
        setGoodsId(builder.goodsId);
        setCoverUrl(builder.coverUrl);
        setCoverNeedUpload(builder.coverNeedUpload);
        setTitle(builder.title);
        setArticleNodeList(builder.articleNodeList);
        setInterestList(builder.interestList);
        setIndustryList(builder.industryList);
        setLocationData(builder.locationData);
        setOperationType(builder.operationType);
        setRteModel(builder.rteModel);
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public String getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(String goodsId) {
        this.goodsId = goodsId;
    }

    public String getCoverUrl() {
        return coverUrl;
    }

    public void setCoverUrl(String coverUrl) {
        this.coverUrl = coverUrl;
    }

    public boolean isCoverNeedUpload() {
        return coverNeedUpload;
    }

    public void setCoverNeedUpload(boolean coverNeedUpload) {
        this.coverNeedUpload = coverNeedUpload;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public List<ArticleNode> getArticleNodeList() {
        return articleNodeList;
    }

    public void setArticleNodeList(List<ArticleNode> articleNodeList) {
        this.articleNodeList = articleNodeList;
    }

    public List<Interest> getInterestList() {
        return interestList;
    }

    public void setInterestList(List<Interest> interestList) {
        this.interestList = interestList;
    }

    public List<Interest> getIndustryList() {
        return industryList;
    }

    public void setIndustryList(List<Interest> industryList) {
        this.industryList = industryList;
    }

    public LocationData getLocationData() {
        return locationData;
    }

    public void setLocationData(LocationData locationData) {
        this.locationData = locationData;
    }

    public int getOperationType() {
        return operationType;
    }

    public void setOperationType(int operationType) {
        this.operationType = operationType;
    }

    public RTEModel getRteModel() {
        return rteModel;
    }

    public void setRteModel(RTEModel rteModel) {
        this.rteModel = rteModel;
    }

    public static PublishInfoFormModel parse(Moment moment, int operationType) {
        List<TagBackup> tagBackupList = moment.getTags();
        LocationData locationData = new LocationData();
        try {
            City city = DummyDataUtil.getCityById(moment.getCityId());
            locationData.setCity(city.getCity());
            locationData.setProvince(city.getProvince());
            locationData.setSuccess(true);
        } catch (Exception e) {

        }
        List<Interest> industryList = new LinkedList<>();
        List<Interest> interestList = new LinkedList<>();
        for (TagBackup tagBackup : tagBackupList) {
            if (tagBackup == null || TextUtils.isEmpty(tagBackup.getId())) continue;
            switch (tagBackup.getGroupType()) {
                case SetupUtil.GROUP_TYPE_AVOCATION:
                    interestList.add(InterestUtil.convertTagBackupToInterest(tagBackup));
                    break;
                case SetupUtil.GROUP_TYPE_CHANNEL:
                    industryList.add(InterestUtil.convertTagBackupToInterest(tagBackup));
                    break;
            }
        }

        String goodsId;
        switch (operationType) {
            case PublishUtil.OPERATION_TYPE_CREATE:
            case PublishUtil.OPERATION_TYPE_COPY:
                goodsId = null;
                break;
            case PublishUtil.OPERATION_TYPE_EDIT:
                goodsId = moment.getId();
                break;
            default:
                goodsId = null;
                break;

        }
        String richText;
        try {
            Article article = JSON.parseObject(moment.getMoreBackup(), Article.class);
            richText = article.getRichText();
            richText = TextUtils.isEmpty(richText) ? "" : richText;
        } catch (Exception e) {
            richText = "";
        }
        DetailBackup detailBackup = moment.getDetailBackup();
        List<Content> contentList = detailBackup.getContent();
        List<ArticleNode> articleNodeList = PublishUtil.convertContentToArticle(contentList);
        return PublishInfoFormModel.newBuilder()
                .goodsId(goodsId)
                .operationType(operationType)
                .title(moment.getTitle())
                .interestList(interestList)
                .industryList(industryList)
                .locationData(locationData)
                .coverNeedUpload(false)
                .coverUrl(ListUtil.isEmpty(moment.getCover()) ? "" : moment.getCover().get(0))
                .articleNodeList(articleNodeList)
                .rteModel(RTEModel.newBuilder()
                        .content(richText)
                        .build())
                .build();
    }

    public static final class Builder {
        private String goodsId;
        private String coverUrl;
        private boolean coverNeedUpload;
        private String title;
        private List<ArticleNode> articleNodeList;
        private List<Interest> interestList;
        private List<Interest> industryList;
        private LocationData locationData;
        private int operationType;
        private RTEModel rteModel;

        private Builder() {
            operationType = PublishUtil.OPERATION_TYPE_CREATE;
            coverNeedUpload = true;
            goodsId = "";
            coverUrl = "";
            title = "";
            articleNodeList = new LinkedList<>();
            interestList = new LinkedList<>();
            industryList = new LinkedList<>();
            locationData = new LocationData();
            rteModel = RTEModel.newBuilder().build();
        }

        public Builder goodsId(String val) {
            goodsId = val;
            return this;
        }

        public Builder coverUrl(String val) {
            coverUrl = val;
            return this;
        }

        public Builder coverNeedUpload(boolean val) {
            coverNeedUpload = val;
            return this;
        }

        public Builder title(String val) {
            title = val;
            return this;
        }

        public Builder articleNodeList(List<ArticleNode> val) {
            articleNodeList = val;
            return this;
        }

        public Builder interestList(List<Interest> val) {
            interestList = val;
            return this;
        }

        public Builder industryList(List<Interest> val) {
            industryList = val;
            return this;
        }

        public Builder locationData(LocationData val) {
            locationData = val;
            return this;
        }

        public Builder operationType(int val) {
            operationType = val;
            return this;
        }

        public Builder rteModel(RTEModel val) {
            rteModel = val;
            return this;
        }

        public PublishInfoFormModel build() {
            return new PublishInfoFormModel(this);
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.goodsId);
        dest.writeString(this.coverUrl);
        dest.writeByte(this.coverNeedUpload ? (byte) 1 : (byte) 0);
        dest.writeString(this.title);
        dest.writeTypedList(this.articleNodeList);
        dest.writeTypedList(this.interestList);
        dest.writeTypedList(this.industryList);
        dest.writeParcelable(this.locationData, flags);
        dest.writeInt(this.operationType);
        dest.writeParcelable(this.rteModel, flags);
    }

    protected PublishInfoFormModel(Parcel in) {
        this.goodsId = in.readString();
        this.coverUrl = in.readString();
        this.coverNeedUpload = in.readByte() != 0;
        this.title = in.readString();
        this.articleNodeList = in.createTypedArrayList(ArticleNode.CREATOR);
        this.interestList = in.createTypedArrayList(Interest.CREATOR);
        this.industryList = in.createTypedArrayList(Interest.CREATOR);
        this.locationData = in.readParcelable(LocationData.class.getClassLoader());
        this.operationType = in.readInt();
        this.rteModel = in.readParcelable(RTEModel.class.getClassLoader());
    }

    public static final Creator<PublishInfoFormModel> CREATOR = new Creator<PublishInfoFormModel>() {
        @Override
        public PublishInfoFormModel createFromParcel(Parcel source) {
            return new PublishInfoFormModel(source);
        }

        @Override
        public PublishInfoFormModel[] newArray(int size) {
            return new PublishInfoFormModel[size];
        }
    };
}
